package com.seanchenxi.gwt.xmlrpc.client.model.impl;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.seanchenxi.gwt.xmlrpc.client.ResponseHandler;
import com.seanchenxi.gwt.xmlrpc.client.model.BaseModel;
import com.seanchenxi.gwt.xmlrpc.client.util.XmlRpcParser;


public class BaseModelResponseHandler<T extends BaseModel> implements ResponseHandler<T>{

	@SuppressWarnings("unchecked")
	@Override
	public T handleResponse(String responseText) {
		try {
			if(responseText != null && responseText.length() > 0){
				GWT.log(responseText);
				Object object = XmlRpcParser.parserResponse(responseText);
				if(object instanceof HashMap<?,?>){
					return (T) new BaseModelImpl((HashMap<String, Object>) object);
				}
			}
			return null;
		} catch (Throwable e) {
			GWT.log("Base Model Response Handler Error", e);
			return null;
		}
	}
}
